package net.tatu.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
	/**
	  * 验证邮箱
	  * @param email
	  * @return
	  */
	 public static boolean isEmail(String email){
	  boolean flag = false;
	  try{
	    String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	    Pattern regex = Pattern.compile(check);
	    Matcher matcher = regex.matcher(email);
	    flag = matcher.matches();
	   }catch(Exception e){
	    flag = false;
	   }
	  return flag;
	 }

	 /**
	  * 验证手机号码
	  * @param mobiles
	  * @return
	  */
	 public static boolean isMobile(String mobileNumber){
	  boolean flag = false;
	  try{
	    Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
	    Matcher matcher = regex.matcher(mobileNumber);
	    flag = matcher.matches();
	   }catch(Exception e){
	    flag = false;
	   }
	  return flag;
	 }
	 
	 public static String getVerifyMobileCode(){
		 String[] ss = {"1","2","3","4","5","6","7","8","9","0"};
		 String ret = "";
		 for(int i = 0; i < 4; i++){
			 int j = (int)(Math.random() * ss.length);
			 ret = ret + ss[j];
		 }
		 return ret;
	 }
	 
	 public static boolean compareTime(Date createDate, String dateTime1,
				String dateTime2) {
		 	boolean bo = false;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			Date date1 = new Date();
			Date date2 = new Date();
			try {
				if(dateTime1.length() < 1 && dateTime2.length() < 1){
					bo = true;
				}else if(dateTime1.length() > 1 && dateTime2.length() < 1){//只限制开始时间
					date1 = sdf.parse(dateTime1);
					if(createDate.getTime() > date1.getTime()){
						bo = true;
					}
				}else if(dateTime1.length() < 1 && dateTime2.length() > 1){//只限制结束时间
					date2 = sdf.parse(dateTime2);
					if(createDate.getTime() < date2.getTime()){
						bo = true;
					}
				}else if(dateTime1.length() > 1 && dateTime2.length() > 1){//两个时间都限制
					date1 = sdf.parse(dateTime1);
					date2 = sdf.parse(dateTime2);
					if(createDate.getTime() > date1.getTime() && createDate.getTime() < date2.getTime()){
						bo = true;
					}
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bo;
		}
	 
	 public static String getTrace(Throwable t) {
	        StringWriter stringWriter= new StringWriter();
	        PrintWriter writer= new PrintWriter(stringWriter);
	        t.printStackTrace(writer);
	        StringBuffer buffer= stringWriter.getBuffer();
	        return buffer.toString();
	    }
	 
	 public static void main(String[] args) {
		 System.out.println("just for testing git merging function");
		System.out.println(isEmail("lll@2"));
		System.out.println(isEmail("lll@2."));
		System.out.println(isEmail("lll@e.c"));
		System.out.println(isMobile("213"));
		System.out.println(isMobile("sdf"));
		System.out.println(isMobile("15002112267"));
		System.out.println(compareTime(new Date(), "2015-04-10 10:46", "2015-04-23 10:46"));
	}
}
